package com.lky.marshallmall.ware.listener;

import com.alibaba.fastjson.TypeReference;
import com.lky.common.to.mq.StockLockedTo;
import com.lky.common.to.mq.WareOrderTaskDetailTo;
import com.lky.common.utils.R;
import com.lky.marshallmall.ware.dao.WareSkuDao;
import com.lky.marshallmall.ware.entity.WareOrderTaskDetailEntity;
import com.lky.marshallmall.ware.entity.WareOrderTaskEntity;
import com.lky.marshallmall.ware.feign.OrderFeignService;
import com.lky.marshallmall.ware.service.WareOrderTaskDetailService;
import com.lky.marshallmall.ware.service.WareOrderTaskService;
import com.lky.marshallmall.ware.service.WareSkuService;
import com.lky.marshallmall.ware.vo.OrderVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by
 * autor : Marshall lky
 * datetime: 2021/6/30 19:29
 */
@Service
@Slf4j
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {

    @Autowired
    private WareSkuDao skuDao;
    @Autowired
    private WareOrderTaskDetailService wareOrderTaskDetailService;
    @Autowired
    private WareOrderTaskService wareOrderTaskService;
    @Autowired
    private OrderFeignService orderFeignService;
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 库存自动解锁
     * @param to
     * @param message
     */
    @RabbitHandler
    public void handleStockLockedRelease(StockLockedTo to, Message message, Channel channel) throws IOException {
        log.info("收到解锁库存的消息。。。");
        try {
            wareSkuService.unlockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }


}
